<?php

namespace app\admin\controller;

use think\Db;

class User extends AdminController
{
    public function index()
    {
        // 原生查询
        $sql = "SELECT * FROM user";
        $list = Db::query($sql);
        $list = Db::query('SELECT * FROM user WHERE id=?', [5]);

        // DB类查询
        $list = Db::table('lover')->order(['id' => 'desc'])->select();
        // 助手函数查询
        $list = db('user')->select();
        dump($list);
    }

    public function insert()
    {
        // 原生插入
//        $result = Db::execute('INSERT INTO user (id,name) VALUES (:id, :name)', ['id' => null, 'name' => 'k1']);

        $data = [
            'name' => 'k3',
            'age' => 18
        ];
        // DB类插入
//        $result = Db::table('user')->insert($data);
        $result = Db::table('user')->insertGetId($data);
        // 助手函数插入
//        $result = db('user')->insertGetId($data);
        var_dump($result);
    }

    public function delete()
    {
        // DB类
//        $result = Db::table('user')->delete(54);

        $result = Db::table('user')->where('name','k1')->delete();

        var_dump($result);
    }

    public function update()
    {
        // DB类
        $result = Db::table('user')->where('id','1')->update(['age'=>19]);

        var_dump($result);
    }
}
